package com.yiwei.hdb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.LoanRecordAdapterNew;
import com.yiwei.hdb.api.callback.LoanRecordContract;
import com.yiwei.hdb.api.requestInfo.LoanRecordRequest;
import com.yiwei.hdb.constant.Const;
import com.yiwei.hdb.interfaces.OnRecyclerViewListener;
import com.yiwei.hdb.model.LoanRecordModel;
import com.yiwei.hdb.view.CircularProgress;

import java.util.ArrayList;
import java.util.List;

/**
 * 借款记录界面
 * Created by Master.Dong on 16/4/10.
 */
public class LoanRecordActivity extends BaseActivity implements LoanRecordContract.View {

    private RecyclerView recyclerView;
    private LoanRecordAdapterNew adapter = null;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<LoanRecordModel.Data> list = new ArrayList<LoanRecordModel.Data>();
    private int lastVisibleItem;
    //    private CircularProgress loading;
    private TextView nothing;
    private LoanRecordRequest api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.loan_record_activity);
        baseInitView(this, "LoadRecordActivity", Const.ViewType.ACTIVITY);
        initView();

    }

    private void initView() {
        api = LoanRecordRequest.create();
        api.attachView(this);
        setTitleText(getResources().getString(R.string.loan_record));
        setBackIntent();

        nothing = (TextView) findViewById(R.id.nothing);
//        loading = (CircularProgress) findViewById(R.id.progressBar);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        //刷新
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        swipeRefreshLayout.setColorSchemeResources(R.color.refresh_color_bule);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(true);
                getLoanRecord(swipeRefreshLayout);
            }
        });
//        showLoading(true);
        getLoanRecord(null);

    }

    /**
     * 是否显示加载框
     *
     * @param
     */
//    private void showLoading(boolean b) {
//        if (b) {
//            if (loading != null) loading.setVisibility(View.VISIBLE);
//        } else {
//            if (loading != null) loading.setVisibility(View.GONE);
//        }
//    }
    @Override
    public void showError() {

    }

    @Override
    public void complete() {
//        showLoading(false);
        if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void onRequestStart() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(true);
        }
    }

    @Override
    public void getLoanRecordInfoSuccess(LoanRecordModel model) {
        if (model.getData().size() <= 0) {
            nothing.setVisibility(View.VISIBLE);
//                    ToastUtil.makeText(getApplicationContext(), getResources().getString(R.string.no_data), Toast.LENGTH_SHORT);
            return;
        }
        list = model.getData();
        adapter = new LoanRecordAdapterNew(LoanRecordActivity.this, list);
        recyclerView.setAdapter(adapter);
        adapter.setOnRecyclerViewListener(new myRecyclerViewListener());
        nothing.setVisibility(View.GONE);
    }


    private class myRecyclerViewListener implements OnRecyclerViewListener {

        @Override
        public void onItemClick(List<?> list, int position) {
            List<LoanRecordModel.Data> l = (List<LoanRecordModel.Data>) list;
//            int status_num = l.get(position).getStatus_num();
            String id = l.get(position).getId();
                Intent intent = new Intent(LoanRecordActivity.this, LoanDetailV2Activity.class);
                intent.putExtra("id", id);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);

        }

        @Override
        public boolean onItemLongClick(List<?> list, int position) {
            return false;
        }
    }

    /**
     * 借款记录
     */
    private void getLoanRecord(final SwipeRefreshLayout refreshLayout) {
        api.getLoanRecordInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        api = null;
    }
}
